<template lang="pug">
	.report-wrap
		.top-tab
			top-tab(@listenClick="getTab" :tabs="tabTitle")
		.report-content
			no-item(text="暂无相关记录" v-if="noItem")
			.report-item(@click="goDetail(item)" v-for="(item,index) in list" :key="index")
				.report-top
					.title {{item.title}}
					.date {{item.date}}
				.report-hos {{item.hospital}}
</template>

<script>
import noItem from '@/components/no-item'
import topTab from '@/components/top-tab'
export default {
	name: 'report',
	data() {
		return {
			tabTitle: ['检查报告', '检验报告'],
			activeIndex: 0,
			list: [],
			noItem: false
		}
	},
	created() {},
	mounted() {
		this.name = this.btoa(this.$route.query.name)
		this.idNumber = this.btoa(this.$route.query.idNumber)
		this.getTestList()
	},
	onShow() {},
	methods: {
		changePerson() {
			this.$router.push({
				path: '/views/doctorRecord/findPatient'
			})
		},
		getTab(index) {
			if (index === this.activeIndex) return false
			if (index === 0) {
				this.getTestList()
			} else {
				this.getInspectList()
			}
			this.activeIndex = index
		},

		async getTestList() {
			//检查报告
			this.wx.showLoading({
				title: '加载中'
			})
			let { iconographyList, testList, list } = []
			await this.api.iconographyApi
				.getIconographyList({
					name: this.name,
					idType: '1',
					idNo: this.idNumber
				})
				.then(data => {
					if (data.code === '200') {
						iconographyList = data.data.filter(item => item.bgsj !== null)
						iconographyList.forEach(item => {
							list.push({
								id: item.id,
								title: item.jcxm,
								hospital: item.zdjgmc,
								date: this.dayjs(item.bgsj.substring(0, 10)).valueOf(),
								testMode: 'icon'
							})
						})
					}
				})
				.catch(err => {
					this.wx.hideLoading()
				})
			await this.api.jtApi
				.getTestList({
					NAME: this.name,
					IDCARD: this.idNumber,
					START_DATE: '',
					END_DATE: '',
					ORG_CODE: '',
					PINDEX: '',
					PSIZE: '',
					QUERYTYPE: '',
					QUERYVALUE: ''
				})
				.then(data => {
					if (data.ret_code === '0') {
						let examreport = this.tools.formatToArray(data.examreport)
						examreport.forEach(item => {
							item.name = this.name
							list.push({
								title: item.EXAMINE_NAME,
								hospital: item.ORG_NAME,
								date: item.SEND_TIME,
								detail: item,
								testMode: 'test'
							})
						})
					}
				})
				.catch(err => {
					this.wx.hideLoading()
				})

			list = _.sortBy(list, item => {
				return item.date
			})
			this.list = list
			this.noItem = list.length <= 0
			this.wx.hideLoading()
		},
		async getInspectList() {
			//检验报告
			this.wx.showLoading({
				title: '加载中'
			})
			let list = []
			this.noItem = false
			await this.api.jtApi
				.getInspectList({
					NAME: this.name,
					CARDID: '',
					CARDTYPE: '',
					IDCARD: this.idNumber,
					ORG_CODE: '',
					START_DATE: '',
					END_DATE: '',
					PATIENT_CODE: '',
					PINDEX: '',
					PSIZE: '',
					QUERYTYPE: '',
					QUERYVALUE: ''
				})
				.then(data => {
					if (data.ret_code === '0') {
						if (data.assayreport) {
							let result = data.assayreport.filter(
								item => item.SEND_TIME !== null
							)
							result.forEach(item => {
								let each = {
									title: item.DESCRIBE,
									hospital: item.ORG_NAME,
									date: item.SEND_TIME,
									queryvalue: item.REPORT_NO,
									org_code: item.ORG_CODE,
									querytype: 'BGDH',
									hospName: item.ORG_NAME,
									name: this.name
								}
								list.push(each)
							})
							list = _.sortBy(list, item => {
								return item.date
							})
						}
						this.list = list
					}
				})
				.catch(() => {})
			this.wx.hideLoading()
			this.noItem = list.length <= 0
		},
		goDetail(item) {
			if (this.activeIndex === 0) {
				if (item.testMode === 'icon') {
					this.wx.setStorage({
						key: 'jumpUrl',
						data: {
							url: 'https://yxllq.jktz.gov.cn/Mobile/Exam/Detail/' + item.id,
							title: '检查报告'
						}
					})
					this.$router.push({
						path: '/views/webview/webview'
					})
				} else {
					this.wx.setStorage({
						key: 'testDetail',
						data: item.detail
					})
					this.$router.push({
						path: '/views/report/testDetail'
					})
				}
			} else {
				//检验报告
				this.$router.push({
					path: '/views/report/inspectDetail',
					query: {
						queryvalue: this.atob(item.queryvalue),
						orgcode: this.atob(item.org_code),
						querytype: this.atob(item.querytype),
						hospname: this.atob(item.hospName),
						name: this.atob(item.name)
					}
				})
			}
		}
	},
	components: {
		noItem,
		topTab
	}
}
</script>

<style lang="stylus">
	.report-wrap
		.top-tab
			height 80px
			line-height 80px
		.report-content
			position absolute
			top 80px
			left 0
			right 0
			bottom 0
			scrollY()
			.report-item
				min-height 158px
				padding 30px
				background-color white
				box-sizing border-box
				position relative
				color blackFontColor
				.report-top
					display flex
					align-items center
					justify-content space-between
					.title
						font-size 32px
						width 100%
						white-space nowrap
						overflow-x hidden
						text-overflow ellipsis
					.date
						text-align right
						min-width 150px
						color #cacaca
						font-size 24px
				.report-hos
					padding-top 20px
					color #aaa
					font-size 24px
					line-height 34px
					&:after
						content ''
						position absolute
						left 30px
						right 30px
						height 2px
						background-color #eee
						bottom 0
</style>
